﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.db.Models;
using com.nm.db.Models.Systems;
using com.nm.library;

namespace com.nm.ApiServer.Attributes {
	/// <summary>
	/// 后台用户权限管理
	/// </summary>
	public class MustRights : Attribute{
		/// <summary>
		/// 目标
		/// </summary>
		public string PrgItem { get; set; }
		/// <summary>
		/// 要检查的权限，默认为 1（0或者没指定时自动变为1）
		/// </summary>
		public long Rights { get; set; }
		/// <summary>
		/// 初始化
		/// </summary>
		/// <param name="PrgItem"></param>
		/// <param name="Rights"></param>
		public MustRights(string PrgItem, long Rights = 1) {
			this.PrgItem = PrgItem;
			this.Rights = Rights;
		}
		/// <summary>
		/// 初始化
		/// </summary>
		/// <param name="PrgItem"></param>
		/// <param name="Rights"></param>
		public MustRights(string PrgItem, RightTypes Rights = RightTypes.View):this( PrgItem, (long)Rights) {

		}
		/// <summary>
		/// 检查是否有权限
		/// </summary>
		/// <param name="StaffNo">对象</param>
		/// <returns>有权限返回true，“拒绝”权限优先</returns>
		public bool Check(string StaffNo) {
            //TODO:暂屏蔽
            //return MemberHelper.HasRights(StaffNo, this.PrgItem, this.Rights);
            return true;
		}
	}
	/// <summary>
	/// 后台用户权限管理
	/// </summary>
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
	public class RoleBcAttribute :Attribute{
		/// <summary>
		/// 角色名称
		/// </summary>
		public string RoleName { get; set; }
		/// <summary>
		/// 目标
		/// </summary>
		public string PrgItem { get; set; }
		/// <summary>
		/// 要检查的权限，默认为 1（0或者没指定时自动变为1）
		/// </summary>
		public long Rights { get; set; }

        /// <summary>
        /// 
        /// </summary>
	    public RoleBcAttribute()
	    {
	        
	    }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="RoleName"></param>
        /// <param name="PrgItem"></param>
        /// <param name="Rights"></param>
        public RoleBcAttribute(string RoleName,string PrgItem, long Rights = 1){
			this.RoleName = RoleName;
			this.PrgItem = PrgItem;
            this.Rights = Rights;
		}
		/// <summary>
		/// 初始化
		/// </summary>
		/// <param name="RoleName"></param>
		/// <param name="PrgItem"></param>
		/// <param name="Rights"></param>
		public RoleBcAttribute(string RoleName, string PrgItem, RightTypes Rights = RightTypes.View):this(RoleName, PrgItem, (long)Rights) {
		
		}
		/// <summary>
		/// 检查是否有权限
		/// </summary>
		/// <param name="StaffNo">对象</param>
		/// <returns>有权限返回true，“拒绝”权限优先</returns>
		public bool Check(string StaffNo){
            //TODO:暂屏蔽
            //return MemberHelper.HasRights(StaffNo, this.PrgItem, this.Rights);
            return true;
		}

        /// <summary>
        /// 检查是否有权限
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
	    public static bool CheckRole(int UserId)
	    {
            using (var dc = new SysEntities())
            {
                var uid = Convert.ToInt32(UserId);
                return (from ur in dc.sys_user_role
                        join r in dc.sys_role on ur.RoleID equals r.ID
                        where (r.RoleType == 3 ||r.RoleType==9) && ur.UserID == UserId
                        select r).Any();
            }
        }
    }
	/// <summary>
	/// 按“控制器”和“动作”检查权限
	/// </summary>
	[AttributeUsage(AttributeTargets.Method)]
	public class RoleBcActionAttribute : RoleBcAttribute{
		/// <summary>
		/// 初始化
		/// </summary>
		/// <param name="RoleName"></param>
		/// <param name="Rights"></param>
		public RoleBcActionAttribute(string RoleName, long Rights) : base(RoleName, string.Empty, Rights) { }
		/// <summary>
		/// 初始化
		/// </summary>
		/// <param name="RoleName"></param>
		/// <param name="Rights"></param>
		public RoleBcActionAttribute(string RoleName, RightTypes Rights):this(RoleName, (long)Rights) { }
		/// <summary>
		/// 检查是否有权限（暂时不支持）
		/// </summary>
		/// <param name="StaffNo">对象</param>
		/// <param name="Controller">控制器</param>
		/// <param name="Action">动作</param>
		/// <returns></returns>
		public bool CheckAction(string StaffNo, string Controller,string Action) {
			if (string.IsNullOrWhiteSpace(StaffNo)) return false;
			if (this.Rights < 1) this.Rights = 1;
			throw new NotSupportedException("暂时不支持");
		}
	}
}
